//
//  GAKKIBaseCollectionViewController.m
//  GANGAIKit
//
//  Created by fh on 2019/11/7.
//  Copyright © 2019 cashchina. All rights reserved.
//

#import "GAKKIBaseCollectionViewController.h"

static NSString* GAKKIBaseCollectionViewCellReuserIdentify = @"GAKKIBaseCollectionViewCellReuserIdentify";
static NSString* GAKKIBaseCollectionElementKindSectionHeaderReuserIdentify = @"GAKKIBaseCollectionElementKindSectionHeaderReuserIdentify";
static NSString* GAKKIBaseCollectionElementKindSectionFooterReuserIdentify = @"GAKKIBaseCollectionElementKindSectionFooterReuserIdentify";

@interface GAKKIBaseCollectionViewController ()
/// 功能性集合视图控件
@property (nonatomic, readwrite, strong) GAKKIFeatureCollectionView *featureCollectionView;
@end

@implementation GAKKIBaseCollectionViewController

#pragma mark - Public

/// 配置子视图:默认添加tableView并设置frame(已适配iPhoneX和iOS11),子类可以重写,重写必须自己添加tableView到父视图并设置frame : 重写父类方法
- (void)setupView {
    [self.view addSubview:self.featureCollectionView];
    [self.featureCollectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@(0));
        if ((self.tabBarController || self.navigationController.tabBarController) && !self.hidesBottomBarWhenPushed) {
            make.bottom.equalTo(@(-kApplicationTabBarHeight));
        }else {
            make.bottom.equalTo(@(0));
        }
//        make.top.equalTo(@(kApplicationTopBarHeight));
        make.top.equalTo(@(0));
    }];
}

/// 刷新数据
- (void)reloadData {
    [self.collectionView reloadData];
}

/// 注册cell:这个方法需要在
/// @param cellClass cell
/// @param identifier 重用符标示
- (void)registerClass:(Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier {
    if (kStringIsEmpty(identifier)) {
        NSString *cellName = NSStringFromClass(cellClass);
        [self.featureCollectionView registerClass:[cellClass class] forCellWithReuseIdentifier:[NSString stringWithFormat:@"%@ReuserIdentify",cellName]];
        return;
    }
    [self.featureCollectionView registerClass:[cellClass class] forCellWithReuseIdentifier:identifier];
}

#pragma mark - Life
+ (instancetype)new {
    return [super new];
}

/// 初始化:这个不能使用到[super initWithParams:params]
- (instancetype)init {
    if (self = [super init]) {
        
    }
    return self;
}

/// 用传递参数初始化
/// @param params 传递参数
- (instancetype)initWithParams:(NSDictionary *)params {
    return [self initWithParams:params settingBlock:nil];
}

/// 用传递参数初始化并自定义属性设置
/// @param params 传递参数
/// @param settingBlock 自定义配置回调
- (instancetype)initWithParams:(NSDictionary *)params settingBlock:(void (^)(GAKKIBaseViewController * _Nonnull))settingBlock {
    return [self initWithDefaultLayoutParams:params featureCollectionViewDelegate:nil customConfigurationSettingBlock:^(GAKKIFeatureCollectionViewConfiguration * _Nonnull settingConfiguration) {
        settingConfiguration.collectionViewLayout = settingConfiguration.systemDefaultLayout;
    } collectionViewDelegate:nil settingBlock:settingBlock];
}

/// 常用初始化方法:使用该方法进行初始化,该方法默认自己是事件的处理，如果是自定义layout需要在子类里遵循对应的协议
/// @param params 传递给页面的参数
/// @param collectionViewSettingBlock 集合视图UI布局配置回调
- (instancetype)initWithDefaultLayoutParams:(NSDictionary *)params customConfigurationSettingBlock:(void (^)(GAKKIFeatureCollectionViewConfiguration * _Nonnull))collectionViewSettingBlock {
    return [self initWithDefaultLayoutParams:params featureCollectionViewDelegate:self customConfigurationSettingBlock:collectionViewSettingBlock collectionViewDelegate:nil settingBlock:nil];
}

/// 初始化方法
/// @param params 传递给页面的参数
/// @param collectionViewSettingBlock 集合视图UI布局配置回调
/// @param settingBlock 自身的配置回调
- (instancetype)initWithDefaultLayoutParams:(NSDictionary * __nullable)params customConfigurationSettingBlock:(void(^ __nullable)(GAKKIFeatureCollectionViewConfiguration * settingConfiguration))collectionViewSettingBlock settingBlock:(void (^ _Nullable)(GAKKIBaseViewController * _Nonnull))settingBlock {
    return [self initWithDefaultLayoutParams:params featureCollectionViewDelegate:self customConfigurationSettingBlock:collectionViewSettingBlock collectionViewDelegate:nil settingBlock:settingBlock];
}

/// 底层初始化方法
/// @param params 传递给页面的参数
/// @param delegate 集合视图的代理:封装UICollectionViewDelegate的代理事件,默认为self,所以一般都是设置为nil
/// @param collectionViewSettingBlock 集合视图UI布局配置回调
/// @param collectionViewDelegate 集合视图代理,默认为self,所以一般都是设置为nil
- (instancetype)initWithDefaultLayoutParams:(NSDictionary *)params featureCollectionViewDelegate:(id<GAKKIFeatureCollectionViewDelegate>)delegate customConfigurationSettingBlock:(void (^)(GAKKIFeatureCollectionViewConfiguration * _Nonnull))collectionViewSettingBlock collectionViewDelegate:(id<UICollectionViewDelegate>)collectionViewDelegate settingBlock:(void (^ _Nullable)(GAKKIBaseViewController * _Nonnull))settingBlock {
    if (self = [super initWithParams:params]) {
        [self inner_baseCollectionViewControllerDefaultInitSetup];
        [self inner_initDefaultFeatureCollectionViewWithfeatureCollectionViewDelegate:delegate customConfigurationSettingBlock:collectionViewSettingBlock collectionViewDelegate:collectionViewDelegate];
        if (settingBlock) {
            settingBlock(self);
        }
    }
    return self;
}

#pragma mark - Private
- (void)inner_baseCollectionViewControllerDefaultInitSetup {
    _shouldMultiSections = NO;
    self.nullDataTitleText = @"暂无数据";
    self.nullDataDescriptionText = @"暂无数据";
}

- (void)inner_initDefaultFeatureCollectionViewWithfeatureCollectionViewDelegate:(id<GAKKIFeatureCollectionViewDelegate>)delegate customConfigurationSettingBlock:(void (^)(GAKKIFeatureCollectionViewConfiguration * _Nonnull))collectionViewSettingBlock collectionViewDelegate:(id<UICollectionViewDelegate>)collectionViewDelegate {
    
    self.featureCollectionView = [[GAKKIFeatureCollectionView alloc] initWithGAKKIFeatureCollectionViewDelegate:delegate customConfigurationSettingBlock:collectionViewSettingBlock collectionViewDelegate:collectionViewDelegate];
    
    _featureCollectionView.collectionView.dataSource = self;
    
    [_featureCollectionView.collectionView registerClass:[GAKKIBaseCollectionViewCell class] forCellWithReuseIdentifier:GAKKIBaseCollectionViewCellReuserIdentify];
    
    [_featureCollectionView.collectionView registerClass:[GAKKIBaseCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:GAKKIBaseCollectionElementKindSectionHeaderReuserIdentify];
    [_featureCollectionView.collectionView registerClass:[GAKKIBaseCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:GAKKIBaseCollectionElementKindSectionFooterReuserIdentify];
}

#pragma mark - UICollectionViewDataSource
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GAKKIBaseCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:GAKKIBaseCollectionViewCellReuserIdentify forIndexPath:indexPath];
    return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.shouldMultiSections) {
        NSArray *subDataSource = self.dataSource[section];
        return subDataSource.count;
    }
    return self.dataSource.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    if (self.shouldMultiSections) {
        return self.dataSource ? self.dataSource.count : 0;
    }
    return 1;
}

#pragma mark - GAKKIFeatureCollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    DBLog(@"%@",indexPath);
}

#pragma mark - Setter and getter
- (UICollectionView *)collectionView {
    return self.featureCollectionView.collectionView;
}

/// 数据源
- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

@end
